package leet.ku._19;

class ListNode {
	int val;
	ListNode next;

	ListNode(int x) {
		val = x;
	}
}

public class Solution {
	public ListNode removeNthFromEnd(ListNode head, int n) {
		ListNode tail = head;
		for (int i = 0; i < n; i++)
			tail = tail.next;
		
		if (tail == null)
			return head.next;
		ListNode parent = head;
		while (tail.next != null) {
			parent = parent.next;
			tail = tail.next;
		}
		parent.next = parent.next.next;
		return head;
	}
}